package com.pda.yuanjing.pages.xsck.scanf;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.JieDianBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.beanout.JDBeanDNewOut;
import com.pda.yuanjing.beanout.ScanfBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ScanfModelImpl extends BaseModel implements ScanfModel {
    private String TAG = "InScanfModelImpl";

    public ScanfModelImpl(Context context) {
        super(context);
    }


    @Override
    public void scanfCode(String  batchID,String jieDianName, String billListID, String codes,String selectMsg, CodeBeans codeBeans, CallBack callBack) {
        JDBeanDNewOut jdBeanOut = new JDBeanDNewOut();
        jdBeanOut.setErrcode(-1);
        jdBeanOut.setErrmsg(SetConfig.getConnectError());
        try {
            JieDianBeanIn jieDianBeanIn = new JieDianBeanIn();
            jieDianBeanIn.setAction("SY_UpdateCode_XXCK");
            jieDianBeanIn.setCid(SharedPreferencesManger.getCid());
            jieDianBeanIn.setUserID(SharedPreferencesManger.getUid());
            jieDianBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            jieDianBeanIn.setBatchID(batchID);
            jieDianBeanIn.setBillListID(billListID);
            jieDianBeanIn.setCode(codes);
            jieDianBeanIn.setSelectMsg(selectMsg);
            jieDianBeanIn.setJieDianName(jieDianName);

            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(jieDianBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                jdBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), JDBeanDNewOut.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            if (jdBeanOut.getErrcode() == 0) {
                callBack.onshowSuccessres(jdBeanOut.getData());
            } else {
                callBack.onshowres(jdBeanOut.getErrmsg());
            }
        }
    }

}

